package com.battcn.platform.service.webmagic;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.battcn.platform.entity.AjaxJson;
import com.battcn.platform.entity.crow.ProxyIp;
import com.battcn.platform.mapper.crow.ProxyIpMapper;
import com.battcn.platform.service.utils.IPCheckUtil;
import com.battcn.platform.service.webmagic.downloader.CrawlerDownloader;
import com.battcn.platform.service.webmagic.downloader.CrowProxyProvider;
import com.battcn.platform.service.webmagic.pageprocessor.HupuBxjPageProcessor;
import com.battcn.platform.service.webmagic.pageprocessor.MovieProcessor;
import com.battcn.platform.service.webmagic.pageprocessor.ProxyPoolProcessor1;
import com.battcn.platform.service.webmagic.pipeline.HupuSpiderPipeline;
import com.battcn.platform.service.webmagic.pipeline.IPSpiderPipeline;
import com.battcn.platform.service.webmagic.pipeline.MoviesSpiderPipeline;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.SpiderListener;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.proxy.Proxy;

@Service
public class CrawlerServeice {
	@Autowired
	IPSpiderPipeline ipSpiderPipeline;

	@Autowired
	HupuSpiderPipeline hupuSpiderPipeline;
	@Autowired
	MoviesSpiderPipeline moviesSpiderPipeline;
	@Autowired
	ProxyIpMapper proxyIpMapper;
	

	// 代理ip测试
	public synchronized void getProxyIP() {
		System.out.println("----------------------getProxyIP----------------------------begin");
		int threadNum = 30;
		ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
		spider=Spider.create(new ProxyPoolProcessor1()).addUrl("http://www.xicidaili.com/nn")
				.thread(executorService, threadNum)
				// .addUrl("http://blog.sina.com.cn/s/articlelist_1487828712_0_1.html")
				.addPipeline(ipSpiderPipeline);
		spider.run();
		System.out.println("----------------------getProxyIP----------------------------end");

	}

	
	// 获取代理ip
	public synchronized List<Proxy> getProxy() {
		getProxyIP();
		List<ProxyIp> proxyList = proxyIpMapper.findAllProxies();
		List<Proxy> proxies = new ArrayList<>(proxyList.size());
		for (ProxyIp proxyIp : proxyList) {
			if(IPCheckUtil.checkValidIP(proxyIp.getIp(), proxyIp.getPort())) {
				proxies.add(new Proxy(proxyIp.getIp(), proxyIp.getPort()));
			}
		}
		return proxies;

	}
	@Autowired
	private Environment env;

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Autowired
	private ObjectMapper objectMapper;

	public void crawlerProduce() throws JsonProcessingException {
		getProxyIP();
		// TODO:真正的录入收款单记录1-mq操作-异步，解耦先有业务模块的整体逻辑，降低整个业务模块的响应速度
		List<ProxyIp> proxyList = proxyIpMapper.findAllProxies();
		if(proxyList!=null&&proxyList.size()>0){
			getProxyIP();
		}
		Message message = MessageBuilder.withBody(objectMapper.writeValueAsBytes(proxyList)).build();
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		// 方式二：exchange+routingKey 携带着消息 -> 队列 ->进行监听消费
		rabbitTemplate.convertAndSend(env.getProperty("register.exchange.name"),
				"te1st2.a", message);
	
		System.out.println(123);
	}

	// 虎扑爬虫测试
	public synchronized void getHUPU() {
		System.out.println("----------------------getHUPU----------------------------begin");
		int threadNum = 30;
		ExecutorService executorService = Executors.newFixedThreadPool(threadNum);

		List<ProxyIp> proxyList = proxyIpMapper.findAllProxies();
		proxyList = proxyList.subList(0, 10);
		List<Proxy> proxies = new ArrayList<>(proxyList.size());
		for (ProxyIp proxyIp : proxyList) {
			proxies.add(new Proxy(proxyIp.getIp(), proxyIp.getPort()));
		}

		HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
		// 设置动态转发代理，使用定制的ProxyProvider
		httpClientDownloader.setProxyProvider(new CrowProxyProvider(proxies));
		// httpClientDownloader.setProxyProvider(CrowProxyProvider.from(new
		// Proxy("forward.xdaili.cn", 80)));
		Spider.create(new HupuBxjPageProcessor())
				// new PostInfoPageProcessor())
				// .setDownloader(httpClientDownloader)
				.addUrl("https://bbs.hupu.com/bxj-1")
				// .addUrl("http://blog.sina.com.cn/s/articlelist_1487828712_0_1.html")
				.addPipeline(hupuSpiderPipeline).thread(executorService, threadNum).run();
		System.out.println("----------------------getHUPU----------------------------end");
	}
	Spider spider;
	// 电影爬虫测试
	public synchronized void getMovies() {
		System.out.println("----------------------getMovies----------------------------begin");
		int threadNum = 5;

		/*List<ProxyIp> proxyList = proxyIpMapper.findAllProxies();
		List<Proxy> proxies = new ArrayList<>(proxyList.size());
		for (ProxyIp proxyIp : proxyList) {
			proxies.add(new Proxy(proxyIp.getIp(), proxyIp.getPort()));
		}
		
		threadNum=proxies.size();
		ExecutorService executorService = Executors.newFixedThreadPool(threadNum);*/
		
		CrawlerDownloader httpClientDownloader = new CrawlerDownloader();
		// 设置动态转发代理，使用定制的ProxyProvider
		//httpClientDownloader.setProxyProvider(new CrowProxyProvider(proxies));
		
		spider=Spider.create(new MovieProcessor())
				// new PostInfoPageProcessor())
				.setDownloader(new CrawlerDownloader())
				.addUrl("http://www.80s.tw/")
				// .addUrl("http://blog.sina.com.cn/s/articlelist_1487828712_0_1.html")
				.addPipeline(moviesSpiderPipeline)
				.thread(threadNum);
				//.thread(3)
		spider.run();
	
		System.out.println("----------------------getMovies----------------------------end");
	}
	
	
	// 电影爬虫测试
		public  void spiderStop() {
			System.out.println("----------------------spiderStop----------------------------begin");
			spider.stop();
			
			System.out.println("----------------------spiderStop----------------------------end");
		}
	
	// 电影爬虫测试
		public synchronized AjaxJson CrawlerByUrl(String url) {
			System.out.println("----------------------CrawlerByUrl----------------------------begin");
			
			spider=Spider.create(new MovieProcessor())
					// new PostInfoPageProcessor())
					.setDownloader( new CrawlerDownloader())
					.addUrl(url)
					// .addUrl("http://blog.sina.com.cn/s/articlelist_1487828712_0_1.html")
					.addPipeline(moviesSpiderPipeline)
					//.thread(3)
					;
			spider.run();
			System.out.println("----------------------CrawlerByUrl----------------------------end");
			return new AjaxJson("爬虫任务执行完毕！", true);
		}
}
